package net.dopan.pigframe.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import lombok.extern.slf4j.Slf4j;
import net.dopan.pigframe.core.base.service.impl.BaseServiceImpl;
import net.dopan.pigframe.core.model.SysZuulRoute;
import net.dopan.pigframe.core.utils.CommonConstant;
import net.dopan.pigframe.user.mapper.SysZuulRouteMapper;
import net.dopan.pigframe.user.service.SysZuulRouteService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package: net.dopan.pigframe.user.service.impl
 * @Title: SysZuulRouteServiceImpl
 * @Description: 动态路由配置表 服务实现类
 * @author: 刘宽
 * @date: 2018/9/2 20:54
 */
@Slf4j
@Service
public class SysZuulRouteServiceImpl extends BaseServiceImpl<SysZuulRouteMapper, SysZuulRoute> implements SysZuulRouteService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 立即生效配置
     *
     * @return
     */
    @Override
    public Boolean applyZuulRoute() {
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        List<SysZuulRoute> routeList = selectList(wrapper);
        redisTemplate.opsForValue().set(CommonConstant.ROUTE_KEY, JSON.toJSONString(routeList));
        //暂未使用
        //rabbitTemplate.convertAndSend(MqQueueConstant.ROUTE_CONFIG_CHANGE, "1");
        log.debug("添加数据库自定义的路由配置,{}", routeList.toString());
        return Boolean.TRUE;
    }

}
